/**
 * 
 */
package com.p2p.front.util;

import java.util.Date;

import com.p2p.base.model.Money;
import com.p2p.base.util.DateUtil;
import com.p2p.front.dal.model.LoanInfo;
import com.p2p.front.dal.model.LoanRepay;
import com.p2p.front.dal.model.TenderReturn;

/**
 * @author ruiqi
 *
 */
public class LoanRepayUtil {

	public static Money caluReturnAmount(TenderReturn tenderReturn,boolean advance, LoanInfo loanInfo,Date now) {
		Long currentTime = now.getTime();
		// 非提前还款
		if (!advance) {
			Date returnTime = tenderReturn.getTime();
			int days = DateUtil.getDayBetweenTwoDate(
					currentTime, returnTime.getTime());

			// 逾期费用
			long dayInterestCent = 0l;
			if (days > 0) {
				// 每日费率
				double dayRate = RateUtil.getDayliRate(loanInfo.getReturnApr());
				// 根据逾期天数计算费用
				dayInterestCent = (long) (tenderReturn.getAmount().getCent() * dayRate);

			}
			return new Money(dayInterestCent).add(tenderReturn.getAmount());

		} else {// 提前还款

			Date startTime = tenderReturn.getStartTime();
			int days = DateUtil.getDayBetweenTwoDate(
					currentTime, startTime.getTime());

			// 利息费用
			long dayInterestCent = 0l;
			if (days > 0) {
				// 每日费率
				double dayRate = RateUtil.getDayliRate(loanInfo.getReturnApr());
				// 根据逾期天数计算费用
				dayInterestCent = (long) (tenderReturn.getAmount().getCent() * dayRate);

			}
			return new Money(dayInterestCent).add(tenderReturn.getPrincipal());
		}

	}
	
	public static Money caluRepayAmount(LoanRepay loanRepay,boolean advance, LoanInfo loanInfo) {
		Long currentTime = System.currentTimeMillis();
		// 非提前还款
		if (!advance) {
			Date returnTime = loanRepay.getTime();
			int days = DateUtil.getDayBetweenTwoDate(
					currentTime, returnTime.getTime());

			// 逾期费用
			long dayInterestCent = 0l;
			if (days > 0) {
				// 每日费率
				double dayRate = RateUtil.getDayliRate(loanInfo.getApr());
				// 根据逾期天数计算费用
				dayInterestCent = (long) (loanRepay.getRepayAmount().getCent() * dayRate);

			}
			return new Money(dayInterestCent).add(loanRepay.getRepayAmount());

		} else {// 提前还款

			Date startTime = loanRepay.getStartTime();
			int days = DateUtil.getDayBetweenTwoDate(
					currentTime, startTime.getTime());

			// 利息费用
			long dayInterestCent = 0l;
			if (days > 0) {
				// 每日费率
				double dayRate = RateUtil.getDayliRate(loanInfo.getApr());
				// 根据逾期天数计算费用
				dayInterestCent = (long) (loanRepay.getRepayAmount().getCent() * dayRate);

			}
			return new Money(dayInterestCent).add(loanRepay.getPrincipal());
		}

	}
	
	/**
	 * 判断是否是同最后一期的还款
	 * @param loanRepay
	 * @param loanInfo
	 * @return
	 */
	public static boolean isLastedLoanRepay(LoanRepay loanRepay,LoanInfo loanInfo){
		int deadLine = loanInfo.getDeadline();
		if(loanRepay.getNo()==deadLine){
			return true;
		}
		return false;
	}
	
	/**
	 * 判断是否产生计息
	 * 
	 * @param loanRepay
	 * @return
	 */
	public static boolean hasInterest(LoanRepay loanRepay){
		//以当前时间和计息日比较，看是否大于0天，如果是说明产生计息，不能提前还款
		int days = DateUtil.getDayBetweenTwoDate(System.currentTimeMillis(), loanRepay.getStartTime().getTime());
		if(days>0){
			return true;
		}
		return false;
	}
	
}
